package com.nxmu.hire.service.impl;

import com.nxmu.hire.common.exception.ServiceException;
import com.nxmu.hire.dao.WorkloadMapper;
import com.nxmu.hire.model.Workload;
import com.nxmu.hire.model.WorkloadExample;
import com.nxmu.hire.model.enums.CollegeEnum;
import com.nxmu.hire.service.WorkloadService;
import com.nxmu.hire.vo.FindAllWorkload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkloadServiceImpl implements WorkloadService {

    private final static Logger LOGGER = LoggerFactory.getLogger(WorkloadServiceImpl.class);

    @Autowired
    private WorkloadMapper workloadMapper;

    /**
     * 添加工作量
     *
     * @param workload
     */
    @Override
    public void save(Workload workload) throws ServiceException {
        try {
            workloadMapper.insert(workload);
        } catch (Exception e) {
            LOGGER.error("该工作量已存在，添加失败");
            throw new ServiceException("该工作量已存在，添加失败");
        }
    }

    /**
     * 查询所有工作量信息
     * @param findAllWorkload
     * @return
     */
    @Override
    public List<Workload> findAllWorkload(CollegeEnum collegeId, FindAllWorkload findAllWorkload) {
        WorkloadExample workloadExample = new WorkloadExample();
        workloadExample.createCriteria().andCollegeIdEqualTo(collegeId);
        return workloadMapper.selectByExample(workloadExample);
    }

    /**
     * 修改工作量
     * @param workloadId
     * @param workloadQuantity
     */
    @Override
    public void modifyWorkload(Integer workloadId, Integer workloadQuantity) throws ServiceException{
        Workload workload = new Workload();
        workload.setWorkloadId(workloadId);
        workload.setWorkloadQuantity(workloadQuantity);
        workloadMapper.updateByPrimaryKeySelective(workload);
    }
}
